package com.cqupt.utils;

import com.cqupt.utils.BaiduTransUtil.TransApi;
import org.aspectj.lang.ProceedingJoinPoint;

import java.util.HashMap;
import java.util.Map;

public class AopAdvice {
    public Map<String, Object> around(ProceedingJoinPoint pjp) throws Throwable {
        Map<String, Object> proceed = (Map<String, Object>) pjp.proceed();
        if (proceed == null){
            return null;
        }
        Object error = proceed.get("error");
        if (null == error){
            Object flag =  proceed.get("flag");
            if (null != flag){
                boolean f = Boolean.parseBoolean(flag.toString());
                if(!f){
                    String message = proceed.get("message").toString();
                    String news = transErrorNews(message);
                    proceed.put("message",news);
                }
            }else {
                return proceed;
            }
        }
        else {
            String news = "提交信息有误，与已创建项存在冲突";
            proceed.put("error",news);
        }
        return proceed;
    }
    /**
    * @Description //翻译错误返回信息，首先遍历本地词库查找错误信息，如不存在则直接返回提示
    * @Date 14:38 2021/3/3
    * @Param [message]错误返回信息
    **/
    private String transErrorNews(String message){
        HashMap<String, String> chineseMap = TextUtils.getDataToMap();
        String news = null;
        if (chineseMap.containsKey(message)){
            String dataChinese = chineseMap.get(message);
            news = dataChinese + "(提交数据存在无效参数)";
        }else {
            news = "提交数据存在无效参数,请重新填写！";
        }
        return news;
    }
}
